package com.larissa.android.quizdemo;

import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

public class QuizViewModel extends ViewModel {
    private static final String TAG="QuizViewModel";
    private static final String CURRENT_INDEX="Current_index";
    private static final String CURRENT_COUNT="Current_count";
    private static final String IS_HELP_KEY="is_help";

    private SavedStateHandle mSavedStateHandle;

    int mCurrentIdx;
    int mCount;
    private boolean mCurrentQuestionAnswer;

    public boolean isCurrentQuestionAnswer() {
        if(mCurrentIdx==-1){
            return mQuestions[mQuestions.length-1].isQuestionAnswer();
        }else
            return mQuestions[mCurrentIdx].isQuestionAnswer();
    }

    public int getCurrentQuestionText() {
        if(mCurrentIdx==-1)
            return mQuestions[mQuestions.length-1].getQuestionId();
        else
            return mQuestions[mCurrentIdx].getQuestionId();
    }

    public void setHelpKey(boolean value){
        mSavedStateHandle.set(IS_HELP_KEY,value);
    }

    public boolean getHelpKey(){
        return mSavedStateHandle.get(IS_HELP_KEY)==null? false: mSavedStateHandle.get(IS_HELP_KEY);
    }

    private int mCurrentQuestionText;


    private Question[] mQuestions=new Question[]{
            new Question(R.string.test_bj,true),
            new Question(R.string.test_dc,false),
            new Question(R.string.test_london,true),
            new Question(R.string.test_tokyo,true)
    };


    public QuizViewModel(SavedStateHandle savedStateHandle){
        mSavedStateHandle=savedStateHandle;
        mCurrentIdx=mSavedStateHandle.get(CURRENT_INDEX)==null? 0: mSavedStateHandle.get(CURRENT_INDEX);
        mCount=mSavedStateHandle.get(CURRENT_COUNT)==null?0:mSavedStateHandle.get(CURRENT_COUNT);
    }


    public void moveToNext(){
        mCurrentIdx++;
        if(mCurrentIdx>=mQuestions.length){
            mCurrentQuestionText=mQuestions[mCurrentIdx-1].getQuestionId();
            mCurrentIdx=-1;
        }else{
            mCurrentQuestionAnswer=mQuestions[mCurrentIdx].isQuestionAnswer();
            mCurrentQuestionText=mQuestions[mCurrentIdx].getQuestionId();
        }
        mSavedStateHandle.set(CURRENT_INDEX,mCurrentIdx);
        mSavedStateHandle.set(CURRENT_COUNT,mCount);

    }
}
